<?php
/**
*OPENSHOP 管理中心聚集页设定程序
*@package  OpenShop
*@version  $Id: processingdynamic.php,v 1.0 2009/07/29 11:18:17 modified $
*@author   杨敬君 <aqkt_436@hotmail.com>
*/
define('IN_OS', true);
require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/cls_dynamic.php');
include_once(ROOT_PATH."includes/fckeditor/fckeditor.php");
$dyLib=new cls_dynamic();
//读取项目库ID
$lib=$_REQUEST['lib'];

/* 检查权限 */
admin_priv('setup_dynamic_tpl');		

$ty=$_REQUEST['ty'];
$sort=$_REQUEST['sort'];
$act=$_REQUEST['act'];
$textnum=$_REQUEST['num'];

//读取XML配置
    $doc = new DOMDocument();
    $doc->load($dyLib->config_file());
    $libs = $doc->getElementsByTagName( "lib" );
	foreach( $libs as $lib_node){
		if($lib==$lib_node->getAttribute("id")){
		     $slibs=$lib_node->getElementsByTagName("data");
	  	      foreach ($slibs as $slib_node =>$key){			
	  	  	   if($ty==$key->getAttribute("datatype")){
                  if($ty<>"5"){//非图文
				    if($sort==$key->getAttribute("Sort")){
	  	  	   	    //$textnum=$key->getAttribute("num");//数量
	  	  		    $height=$key->getAttribute("height");//高度
			  		$width=$key->getAttribute("width");//宽度
	  	  		    $islink=$key->getAttribute("islink");//是否链接1是0否
	  	  		    $Sort=$key->getAttribute("Sort");//排序				
	  	  		    $length=$key->getAttribute("length");//文本框串长度
	  	  		       if($ty=="1" or $ty=="2"){
	  	  		    	    $target=$key->getAttribute("target");//除图片/flash/html/图文 外的链接目标
	  	  		       }
				     }					
                  }else{ //图文
				  
				     if($sort==$key->getAttribute("Sort")){				   
                       $imglibs=$lib_node->getElementsByTagName("img");//读取图文(图片)
	  	  		       foreach ($imglibs as $aimglibs =>$imgkey){
					   if($sort==$imgkey->getAttribute("Sort")){		
	  	  		       	     $imgheight=$imgkey->getAttribute("height");	//图片高度
						     //echo $imgheight;
	  	  		       	     $imgwidth=$imgkey->getAttribute("width");    //图片宽度
	  	  		       	     $imgislink=$imgkey->getAttribute("islink");  //图片是否链接1是0否
	  	  		       	     $imgtarget=$imgkey->getAttribute("target");  //图片链接目标
						   }	
				 	   }		
					   
	  	  		       $textlibs=$lib_node->getElementsByTagName("text");//读取图文(文字)					   
	  	  		       foreach ($textlibs as $atextlibs =>$textkey){		
					   if($sort==$textkey->getAttribute("Sort")){					       
	  	  		       	    $atextnum=$textkey->getAttribute("num");//读取文字多少条数
	  	  		       	    $atislink=$textkey->getAttribute("islink");//读取文字多少条数
	  	  		       	    $atexttarget=$textkey->getAttribute("target");//文字链接目标	
							}					
						}
					}	
	  	  	     }
			   }
	  	   }
		}
	}

$typevalue=$dyLib->get_dynamicDB($sort,$lib,$ty,$textnum,$atextnum);//类型的值
//执行sql操作
switch ($act){
	case "add":
if($ty=="1"){//文字
		for($i=1;$i<=$textnum;$i++){
			//读取配置文件是否需要链接 如果需要 则验证url是否为空
			if($islink==1){
				if($_REQUEST["dy_TextCUrl$i"]==""){
			    $link[0]['text'] = '返回上一级';
	            $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
	            $er="链接".$i."不能为空";
	            sys_msg($er, 0, $link);
			    }
			 }
			 if($_REQUEST["dy_Text$i"]==""){
			   $link[0]['text'] = '返回上一级';
	           $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
	           $er="标题".$i."不能为空";
	           sys_msg($er, 0, $link);
			 }
		}
		switch ($dyLib->is_dyOnly($lib,$ty,$sort)){
			case 1://更新数据
		    $sql="update ".$os->table('dynamic')." set ";
		    for($i=1;$i<=$textnum;$i++){
		          $sql=$sql."dy_text".$i."='".$_REQUEST["dy_Text$i"]."',";
		       if($i<>$textnum){
		          $sql=$sql."dy_TextCUrl".$i."='".$_REQUEST["dy_TextCUrl$i"]."',";
		       }else {
		   	      $sql=$sql."dy_TextCUrl".$i."='".$_REQUEST["dy_TextCUrl$i"]."'";
		       }
		    }
		    $sql=$sql." where dy_Project='$lib' and dy_sort='$sort'";
		    break;
		    default://添加数据
		    $sql="insert into ".$os->table('dynamic')."(dy_Project,dy_type,dy_sort,";
		    for($i=1;$i<=$textnum;$i++){
		    $sql=$sql."dy_text".$i.",";
		 	  if($i<>$textnum){
		        $sql=$sql."dy_TextCUrl".$i.",";
			  }else{
		        $sql=$sql."dy_TextCUrl".$i."";
			  }
		    };
		    $sql=$sql.") values ('$lib','$ty','$sort',";
		    for($i=1;$i<=$textnum;$i++){
               $sql=$sql."'".$_REQUEST["dy_Text$i"]."',";
			if($i<>$textnum){
		      $sql=$sql."'".$_REQUEST["dy_TextCUrl$i"]."',";
			}else{
		      $sql=$sql."'".$_REQUEST["dy_TextCUrl$i"]."'";
			}
		    };
		    $sql=$sql.")";
		    break;
		}
		$db->query($sql);
		$link[0]['text'] = '返回上一级';
	    $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		sys_msg('设置成功！', 0, $link);
		break;
}elseif ($ty=="2"){//图片
			for($i=1;$i<=$textnum;$i++){
				//验证配置文件是否需要链接 如果需要 则验证url是否为空
				if($islink==1){
					if($_POST["dy_PCUrl$i"]==""){
					$link[0]['text'] = '返回上一级';
	                $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
	                $er="url".$i."不能为空";
	                sys_msg($er, 0, $link);
				    }
				}
			 }
             switch ($dyLib->is_dyOnly($lib,$ty,$sort)){
			 		case 1:	//更新数据			 		
			            $sql="update ".$os->table('dynamic')." set ";
		                for($i=1;$i<=$textnum;$i++){
		                    if(@empty($_FILES["dy_PPath$i"]["name"])==FALSE){
		                	//	$sql=$sql."dy_PPath".$i."='$annexFolder/".$img->upLoad("dy_PPath$i",$i)."',";
		                	$sql=$sql."dy_PPath".$i."='".$dyLib->ftpupfile($_FILES["dy_PPath$i"],$ty)."',";
		                	}
		                if($i<>$textnum){
		                  $sql=$sql."dy_PCUrl".$i."='".$_POST["dy_PCUrl$i"]."',";
		                }else {
		   	              $sql=$sql."dy_PCUrl".$i."='".$_POST["dy_PCUrl$i"]."'";
		                }
		                }
		                 $sql=$sql." where dy_Project='$lib' and dy_sort='$sort'";
			 		break;
			 		default://添加数据
			            $sql="insert into ".$os->table('dynamic')."(dy_Project,dy_type,dy_sort,";
		                for($i=1;$i<=$textnum;$i++){
		                	if(@empty($_FILES["dy_PPath$i"]["name"])==FALSE){
		                		$sql=$sql."dy_PPath".$i.",";
		                	}
		                	if($i<>$textnum){
		                		$sql=$sql."dy_PCUrl".$i.",";
		                	}else{
			            	$sql=$sql."dy_PCUrl".$i."";
		                	}
		                };
		                $sql=$sql.") values ('$lib','$ty','$sort',";
		                for($i=1;$i<=$textnum;$i++){
		                	if(@empty($_FILES["dy_PPath$i"]["name"])==FALSE){
		                	$sql=$sql."'".$dyLib->ftpupfile($_FILES["dy_PPath$i"],$ty)."',";
		                	}
			            if($i<>$textnum){
		                  $sql=$sql."'".$_POST["dy_PCUrl$i"]."',";
			            }else{
		                  $sql=$sql."'".$_POST["dy_PCUrl$i"]."'";
			            }
		                };
		                $sql=$sql.")";
			 			break;
			 	}
			    $db->query($sql);
		        $link[0]['text'] = '返回上一级';
	            $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		        sys_msg('设置成功！', 0, $link);
}elseif ($ty=="3"){		//flash
			 switch ($dyLib->is_dyOnly($lib,$ty,$sort)){
			 		case 1:	//更新数据
			 		if(@empty($_FILES["flaname"]["name"])==FALSE){
			        $sql="update ".$os->table('dynamic')." set dy_FPath='".$dyLib->ftpupfile($_FILES["flaname"],3)."' where dy_Project='$lib' and dy_sort='$sort'";
                    $db->query($sql);
			 		}else {
			 		$link[0]['text'] = '返回上一级';
	                $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		            sys_msg('请上传文件！', 0, $link);
			 		}
			        break;
			 		default://添加数据
			 		if(@empty($_FILES["flaname"]["name"])==FALSE){
			        $sql="insert into ".$os->table('dynamic')."(dy_Project,dy_type,dy_sort,dy_FPath) values('$lib','$ty','$sort','".$dyLib->ftpupfile($_FILES["flaname"],3)."')";
			 		$db->query($sql);
			 		}else{
			 	    $link[0]['text'] = '返回上一级';
	                $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		            sys_msg('请上传文件！', 0, $link);
			 		}
		            break;
			 	}
		        $link[0]['text'] = '返回上一级';
	            $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		        sys_msg('设置成功！', 0, $link);
}elseif ($ty=="4"){//html
			      if($_REQUEST["HTMLN"]==""){
			 		 $link[0]['text'] = '返回上一级';
	                 $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
	                 $er="HTML内容不能为空";
	                 sys_msg($er, 0, $link);
			 	   }
			   switch ($dyLib->is_dyOnly($lib,$ty,$sort)){
			 		case 1:	//更新数据
			        $sql="update ".$os->table('dynamic')." set dy_Html='".$_REQUEST["HTMLN"]."' where dy_Project='$lib' and dy_sort='$sort'";
			 		break;
			 		default:	//添加数据
			        $sql="insert into ".$os->table('dynamic')."(dy_Project,dy_type,dy_sort,dy_Html) values('$lib','$ty','$sort','".$_REQUEST["HTMLN"]."')";
		            break;
			 	}
			 	$db->query($sql);
		        $link[0]['text'] = '返回上一级';
	            $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		        sys_msg('设置成功！', 0, $link);
		}else{//图文
		for($i=1;$i<=$atextnum;$i++){
				//验证配置文件是否需要链接 如果需要 则验证url是否为空
			if($atislink==1){
				if($_REQUEST["dy_TextCUrl$i"]==""){
			    $link[0]['text'] = '返回上一级';
	            $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
	            $er="链接".$i."不能为空";
	            sys_msg($er, 0, $link);
			    }
			 }
			 if($_REQUEST["dy_Text$i"]==""){
			   $link[0]['text'] = '返回上一级';
	           $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
	           $er="标题".$i."不能为空";
	           sys_msg($er, 0, $link);
			 }
		}
		switch ($dyLib->is_dyOnly($lib,$ty,$sort)){
			case 1://更新数据
		    $sql="update ".$os->table('dynamic')." set ";
			if(@empty($_FILES["dy_PPath1"]["name"])==FALSE){
			$sql=$sql." dy_PPath1='".$dyLib->ftpupfile($_FILES["dy_PPath1"],5)."'," ;
			}
			$sql=$sql."dy_PCUrl1='".$_POST["dy_PCUrl1"]."',";
		    for($i=1;$i<=$atextnum;$i++){
		          $sql=$sql."dy_text".$i."='".$_REQUEST["dy_Text$i"]."',";
		       if($i<>$atextnum){
		          $sql=$sql."dy_TextCUrl".$i."='".$_REQUEST["dy_TextCUrl$i"]."',";
		       }else {
		   	      $sql=$sql."dy_TextCUrl".$i."='".$_REQUEST["dy_TextCUrl$i"]."'";
		       }
		    }
		          $sql=$sql." where dy_Project='$lib' and dy_sort='$sort'";
		          break;
		    default://添加数据
		    $sql="insert into ".$os->table('dynamic')."(dy_Project,dy_type,dy_sort,dy_PPath1,dy_PCUrl1,";
		    for($i=1;$i<=$atextnum;$i++){
		    $sql=$sql."dy_text".$i.",";
		 	  if($i<>$atextnum){
		        $sql=$sql."dy_TextCUrl".$i.",";
			  }else{
		        $sql=$sql."dy_TextCUrl".$i."";
			  }
		    };
		    $sql=$sql.") values ('$lib','$ty','$sort','".$dyLib->ftpupfile($_FILES["dy_PPath1"],5)."','".$_POST["dy_PCUrl1"]."',";
		    for($i=1;$i<=$atextnum;$i++){
               $sql=$sql."'".$_REQUEST["dy_Text$i"]."',";
			if($i<>$atextnum){
		      $sql=$sql."'".$_REQUEST["dy_TextCUrl$i"]."',";
			}else{
		      $sql=$sql."'".$_REQUEST["dy_TextCUrl$i"]."'";
			}
		    };
		    $sql=$sql.")";
		    break;
		}
			$db->query($sql);
		    $link[0]['text'] = '返回上一级';
	        $link[0]['href'] = "processingdynamic.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		    sys_msg('设置成功！', 0, $link);
		}
	default:
		break;
}
/*
 * 文字呈现
 */
function text($i,$value){
	    global $islink;
	    if($islink==0){
	    	$re="readonly=false";
	    }
		for($a=1;$a<=$i;$a++){
	    $result=$result."<tr>
	      <td width='8%' class='first-cell'>标题：</td>
	      <td><input type='text' name='dy_Text$a' value=".$value["Text$a"]."></td>
	      <td width='8%'>链接：</td>
	      <td colspan='3'><input $re type='text' name='dy_TextCUrl$a' value=".$value["Url$a"]."></td>
	      </tr>
	      ";
		}
		return $result;
}
/**
 * 图片呈现
 */
function image($i,$h,$w,$value){
	global $_CFG;

	    global $islink;
		global $_CFG;
	    if($islink==0){
	    	$re="readonly=false";
	    }
		for($a=1;$a<=$i;$a++){
	    $result=$result."<tr>
        <td class='first-cell'>图片".$a."：</td>
        <td><img src=".$_CFG['pre_fetch_img'].$value["dy_PPath$a"]."?400,150 >  
        <input type='file' name='dy_PPath$a' size=45> </td>
        <td>高度：</td>
        <td width='7%'>$h.px</td>
        <td width='9%'>宽度：</td>
        <td>$w.px</td>
        </tr><tr>
          <td class='first-cell'>Url".$a.":</td>
          <td colspan=5><input $re type=text name='dy_PCUrl$a' size=57 value=".$value["dy_PCUrl$a"]."></td>
        </tr>";
		}
		return $result;
}
/**
 * flash呈现
 */
function flash($i,$h,$w,$value){
		global $_CFG;
		for($a=1;$a<=$i;$a++){
	    $result=$result."<tr>
        <td class='first-cell'>flash：</td>
        <td colspan='7'>
        <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0 width=$w height=$h>
          <param name=movie value=".$_CFG['pre_fetch_img']."$value[dy_FPath]>
          <param name=quality value=high>
          <embed src=".$_CFG['pre_fetch_img']."$value[dy_FPath] quality=high pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=$w height=$h></embed>
        </object>
        <input type='file' name='flaname'>
        高度：$h.px&nbsp;宽度:$w.px
        </td>
        </tr>";
		}
		return $result;
}
/**
 * 图文呈现
 *
 */
function imtext($h,$w,$atextnum,$value){
	    global $imgislink;
	    global $atislink;
		global $_CFG;
	    if($imgislink==0){
	    	$re="readonly=false";
	    }
	    if($atislink==0){
	    	$ati="readonly=false";
	    }
	    $result="<tr>
                 <td class='first-cell'>图片</td>
                 <td>
                 <img src= ".$_CFG[pre_fetch_img].$value[dy_PPath1]."?400,150 >
                 <input type='file' name='dy_PPath1' size=30></td>
                 <td>高度</td>
                 <td>$h.px</td>
                 <td>宽度</td>
                 <td colspan='3'>$w.px</td>
                 </tr>
                 <tr>
                 <td class='first-cell'>Url</td>
                 <td colspan=7><input $re type=text name=dy_PCUrl1 size=46 value=$value[dy_PCUrl1]></td></tr>";
         for($b=1;$b<=$atextnum;$b++){
           $result=$result."<tr>
                            <td class='first-cell'>文字</td>
                            <td><input type='text' name='dy_Text$b' maxlength='8' value=".$value["Text$b"]."></td>
                            <td colspan='3'>文字链接</td>
                            <td colspan='3'><input $ati type='text' name='dy_TextCUrl$b' value=".$value["Url$b"]."></td>
                            </tr>";
          }
		return $result;
}

 /* 编辑器 */
$editor = new FCKeditor('HTMLN');
$editor->BasePath   = "../includes/fckeditor/";
$editor->ToolbarSet = "Normal";
$editor->Width      = "95%";
$editor->Height     = "300";
$editor->Value      = $typevalue[dy_Html];
$fckeditor = $editor->CreateHtml();
$smarty->assign('text',text($textnum,$typevalue));
$smarty->assign('image',image($textnum,$height,$width,$typevalue));
$smarty->assign('flash',flash($textnum,$height,$width,$typevalue));
$smarty->assign('imtext',imtext($imgheight,$imgwidth,$atextnum,$typevalue));
$smarty->assign('fckeditor', $fckeditor);
$smarty->assign('ur_here','<a href=template.php?act=setup>设置模板</a> >><a href=createdynamic.php?id='.$lib.'>填充'.$_LANG['template_libs'][$lib].'</a> >>设置'.$_LANG['template_libs'][$lib].$dyLib->get_typestring($ty));
$smarty->assign('textnum',$textnum);
$smarty->assign('guide',$guide);
$smarty->assign('type',$ty);
$smarty->assign('lib',$lib);
$smarty->assign('sort',$sort);
$smarty->display("processingdynamic.tpl");

?>